package com.ys.data.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.annotation.ApiLogDetail;
import com.common.base.BaseEnum;
import com.common.exception.BaseException;
import com.common.vo.RoleVo;
import com.ys.data.auth.entity.Role;
import com.ys.data.auth.mapper.RoleMapper;
import com.ys.data.auth.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
* @version: V1.0
* @author: szw
* @className: RoleServiceImpl
* @packageName: RoleServiceImpl
* @description:  角色接口实现
* @data: 2021-1-19 
**/ 
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    /**
     * 添加角色信息
     * @param roleVo
     * @return
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "鉴权服务-添加角色信息",operationType=BaseEnum.INSERT)
    public int saveRole(RoleVo roleVo) {
        if (Objects.isNull(roleVo)){
            throw  new BaseException(BaseEnum.DATA_IS_NULL);
        }
        try {
            Role role = new Role();
            BeanUtils.copyProperties(roleVo,role);
            return saveToUpdateRole(role,true);
        }catch (Exception e){
            throw new BaseException(BaseEnum.ADD_ROLE_ERROR,e);
        }
    }

    /**
     * 更新角色信息
     * @param roleVo
     * @return
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "鉴权服务-更新角色信息",operationType=BaseEnum.UPDATE)
    public int updateRole(RoleVo roleVo) {
        if (Objects.isNull(roleVo)){
            throw  new BaseException(BaseEnum.DATA_IS_NULL);
        }
        try {
            Role role = new Role();
            BeanUtils.copyProperties(roleVo,role);
            return saveToUpdateRole(role,false);
        }catch (Exception e){
            throw new BaseException(BaseEnum.UPDATE_ROLE_ERROR,e);
        }
    }

    /**
     * 查询角色信息
     * @param  roleId
     * @return
     */
    @Override
    @ApiLogDetail(detail = "鉴权服务-查询角色信息",operationType=BaseEnum.SELECT)
    public Role findRole(Long roleId) {
        if (null==roleId){
            throw  new BaseException(BaseEnum.DATA_IS_NULL);
        }
        try {
            return roleMapper.selectById(roleId);
        }catch (Exception e){
            throw new BaseException(BaseEnum.FIND_ROLE_ERROR,e);
        }
    }

    /**
     * 删除角色信息
     * @param roleId
     * @return
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "鉴权服务-删除角色信息",operationType=BaseEnum.DELETE)
    public int deleteRole(Long roleId) {
        if (null == roleId){
            throw  new BaseException(BaseEnum.DATA_IS_NULL);
        }
        try {
            return roleMapper.deleteById(roleId);
        }catch (Exception e){
            throw new BaseException(BaseEnum.DELETE_ROLE_ERROR,e);
        }
    }

    /**
     * 查询角色列表信息接口实现
     * @param roleVo
     * @return
     */
    @Override
    @ApiLogDetail(detail = "鉴权服务-查询角色列表信息接口实现",operationType=BaseEnum.SELECT)
    public List<Role> selectRoles(RoleVo roleVo) {
        if (Objects.isNull(roleVo)){
            throw  new BaseException(BaseEnum.DATA_IS_NULL);
        }
        try {
            QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("enable",roleVo.getEnable());
            return roleMapper.selectList(queryWrapper);
        }catch (Exception e){
            throw new BaseException(BaseEnum.FIND_ROLE_ERROR,e);
        }
    }

    /**
     * flag true 新增 false 更新
     * @param role
     * @param flag
     * @return
     */
    private int saveToUpdateRole(Role role,Boolean flag){
        if (flag){
          return   roleMapper.insert(role);
        }
        return roleMapper.updateById(role);
    }
}
